package com.xky.ui.module.productManagement.select;

import com.xky.pojo.ProductCategories;
import com.xky.utils.DBUtils;
import lombok.Getter;
import lombok.Setter;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Setter
@Getter
public class SelectCateGory {

    public static List<ProductCategories> selectAllCateGory(){
        Connection connection;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<ProductCategories> productCategoriesList = new ArrayList<>();

        connection = DBUtils.getConnection();
        try {
            preparedStatement = connection.prepareStatement("select * from product_categories");
            resultSet = preparedStatement.executeQuery();
            
            // 直接使用while循环处理结果集
            while (resultSet.next()){
                ProductCategories productCategories = new ProductCategories();
                productCategories.setCategoryId(resultSet.getInt("category_id"));
                productCategories.setCategoryName(resultSet.getString("category_name"));
                productCategories.setParentId(resultSet.getInt("parent_id"));
                productCategories.setIsActive(resultSet.getString("is_active"));
                
                // 获取创建时间和更新时间
                if (resultSet.getTimestamp("created_time") != null) {
                    productCategories.setCreatedTime(resultSet.getTimestamp("created_time").toLocalDateTime());
                }
                if (resultSet.getTimestamp("updated_time") != null) {
                    productCategories.setUpdatedTime(resultSet.getTimestamp("updated_time").toLocalDateTime());
                }
                
                productCategoriesList.add(productCategories);
            }
            return productCategoriesList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtils.closeAll(connection,preparedStatement,resultSet);
        }
    }

    public static Boolean selectAllCateGoryById(Integer id){
        Connection connection;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        connection = DBUtils.getConnection();
        try {
            preparedStatement = connection.prepareStatement("select * from product_categories where category_id = ?");
            preparedStatement.setInt(1,id);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    public static List<ProductCategories> selectCateGoryById(Integer id){
        Connection connection;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<ProductCategories> productCategoriesList = new ArrayList<>();

        connection = DBUtils.getConnection();
        try {
            preparedStatement = connection.prepareStatement("select * from product_categories where category_id = ?");
            preparedStatement.setInt(1,id);
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()){
                ProductCategories productCategories = new ProductCategories();

                productCategories.setCategoryId(resultSet.getInt("category_id"));
                productCategories.setCategoryName(resultSet.getString("category_name"));
                productCategories.setParentId(resultSet.getInt("parent_id"));
                productCategories.setIsActive(resultSet.getString("is_active"));
                
                // 获取创建时间和更新时间
                if (resultSet.getTimestamp("created_time") != null) {
                    productCategories.setCreatedTime(resultSet.getTimestamp("created_time").toLocalDateTime());
                }
                if (resultSet.getTimestamp("updated_time") != null) {
                    productCategories.setUpdatedTime(resultSet.getTimestamp("updated_time").toLocalDateTime());
                }
                
                productCategoriesList.add(productCategories);
            }
            return productCategoriesList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtils.closeAll(connection,preparedStatement,resultSet);
        }
    }

    /**
     * 根据分类名称进行模糊查询
     * @param keyword 关键词
     * @return 分类列表
     */
    public static List<ProductCategories> searchCategoriesByKeyword(String keyword) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<ProductCategories> categoriesList = new ArrayList<>();

        try {
            connection = DBUtils.getConnection();
            // 使用LIKE进行模糊查询
            preparedStatement = connection.prepareStatement(
                "SELECT * FROM product_categories WHERE category_name LIKE ?"
            );
            String searchPattern = "%" + keyword + "%";
            preparedStatement.setString(1, searchPattern);
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                ProductCategories category = new ProductCategories();
                category.setCategoryId(resultSet.getInt("category_id"));
                category.setCategoryName(resultSet.getString("category_name"));
                category.setParentId(resultSet.getInt("parent_id"));
                category.setIsActive(resultSet.getString("is_active"));
                
                // 获取创建时间和更新时间
                if (resultSet.getTimestamp("created_time") != null) {
                    category.setCreatedTime(resultSet.getTimestamp("created_time").toLocalDateTime());
                }
                if (resultSet.getTimestamp("updated_time") != null) {
                    category.setUpdatedTime(resultSet.getTimestamp("updated_time").toLocalDateTime());
                }
                
                categoriesList.add(category);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.closeAll(connection, preparedStatement, resultSet);
        }
        return categoriesList;
    }
}
